<?php

namespace common\models;

use Yii;
use common\components\ActiveRecord2015;

/**
 * This is the model class for table "{{%sys_admin_user}}".
 *
 * @property integer $id
 * @property string $user_name
 * @property string $email
 * @property string $password
 * @property integer $reg_time
 * @property string $last_ip
 * @property integer $last_login_time
 * @property integer $site_id
 */
class SysAdminUser extends ActiveRecord2015
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%sys_admin_user}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['reg_time', 'last_login_time', 'site_id'], 'integer'],
            [['user_name', 'password'], 'string', 'max' => 32],
            [['email'], 'string', 'max' => 128],
            [['last_ip'], 'string', 'max' => 16],
               [['user_name'], 'unique']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '编号',
            'user_name' => '用户名称',
            'email' => '邮箱',
            'password' => '密码',
            'reg_time' => '注册时间',
            'last_ip' => '最后登录IP',
            'last_login_time' => '最后登录时间',
            'site_id' => '站点',
        ];
    }
    
    /**
     * 
     * @Title: getAdminUserNameById
     * @Description: 根据管理员的id获取管理员的名称
     * @return: Ambigous <number, unknown>
     * @author: yulong.wang
     * @date: 2014-12-25 下午11:18:09
     */
    public static function getAdminUserNameById($admin_user_id){
    	$admin_user = SysAdminUser::find()->where(['id'=>$admin_user_id])->asArray()->one();
    	if($admin_user){
    		$admin_user_name = $admin_user['user_name'];
    	}else{
    		$admin_user_name = "-";
    	}
    	return $admin_user_name;
    }
    
    /**
     * 
     * @Title: getAdminUserNameSuppById
     * @Description: 根据管理员的id获取管理员的名称，没有则显示供应商
     * @return: Ambigous <number, unknown>
     * @author: weijuan.huang
     * @date: 2015-8-25 下午15:54:33
     */
    public static function getAdminUserNameSuppById($admin_user_id){
    	$admin_user = SysAdminUser::find()->where(['id'=>$admin_user_id])->asArray()->one();
    	if($admin_user){
    		$admin_user_name = $admin_user['user_name'];
    	}else{
    		$admin_user_name = "供应商";
    	}
    	return $admin_user_name;
    }
    
    /**
     * 
     * @Title: getAdminUserNameById
     * @Description: 根据管理员的id获取管理员的名称
     * @return: Ambigous <number, unknown>
     * @author: yulong.wang
     * @date: 2014-12-25 下午11:18:09
     */
    public static function getAdminUserRule($admin_user_id){
        $sql = 'select item_name from dh_sys_auth_assignment where user_id = '.$admin_user_id;
        $data = SysAdminUser::findBySql($sql)->asArray()->all();
        $res = 0;
        foreach ($data as $value){
            if($value['item_name'] == '超级管理员'){
                $res = 1;
            }
        }
        if($res == 1){
            return 1;
        }else{
            return 0;
        }
    }
}
